package com.dsp.soundscape.server;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.dsp.soundscape.model.AlbumInfo;
import com.dsp.soundscape.model.SongInfo;
import com.dsp.soundscape.model.VersionInfo;
import com.dsp.util.ImageShrinker;
import com.dsp.util.QueryData;

public class SoundscapeLoader implements Serializable {
	private static final long serialVersionUID = -8516781950851855518L;

	private static Logger log = Logger.getLogger(SoundscapeLoader.class.toString());
	
	private List <String> parents;
	
	private String temp;
	private String zips;
	
	private String prefix;
	private String pathroot;
	
	private String thumbdir;
	private String thumburi;

	private VersionInfo version = new VersionInfo();
	
	public String getPathroot() {
		return pathroot;
	}

	public void setPathroot(String pathroot) {
		log.info("Root Path: " + pathroot);
		this.pathroot = pathroot;
	}
	
	public String getTemp() {
		return temp;
	}
	public void setTemp(String temp) {
		log.info("Temp Directory (for Zipper): " + temp);
		this.temp = temp;
	}
	
	public String getZips() {
		return zips;
	}
	public void setZips(String zips) {
		log.info("Zipper Apache Alias: " + zips);
		this.zips = zips;
	}
	
	public String getPrefix() {
		return prefix;
	}
	public void setPrefix(String prefix) {
		log.info("Root Apache Alias: " + prefix);
		this.prefix = prefix;
	}
	
	public String getThumbdir() {
		return thumbdir;
	}
	public void setThumbdir(String thumbdir) {
		this.thumbdir = thumbdir;
	}

	public String getThumburi() {
		return thumburi;
	}
	public void setThumburi(String thumburi) {
		this.thumburi = thumburi;
	}

	public List<String> getFolders() {
		if (parents != null) {
			log.info("Scanning Music Paths");
			
			List <String> paths = new ArrayList <String>();
			for (String parent : parents) {
				File parentDir = new File(pathroot + parent);
				File[] dirs = parentDir.listFiles();
				for (File dir : dirs) {
					if (dir.isDirectory()) {
						paths.add(parent + "/" + dir.getName());
					}
				}
			}
			return paths;
		} else {
			log.warning("No Parent Dirs");
			return null;
		}
	}
	
	public void populate(AlbumInfo info) {
		File parentDir = new File (pathroot + "/" + info.getFullpath());
		log.fine("Populating Album Information: " + pathroot + "/" + info.getFullpath());
		
		File thumbs = new File(thumbdir);
		if (!thumbs.exists()) {
			log.info("Creating thumbnail directory: " + thumbdir);
			thumbs.mkdirs();
		}
		
		if (parentDir != null) {
			File[] files = parentDir.listFiles();
			if (files != null) {
				for (File file : files) {
					String fnlc = file.getName().toLowerCase();
					
					if ((fnlc.endsWith(".jpg") || fnlc.endsWith(".jpeg")) && info.getImage() == null) {
						info.setImage(prefix + "/" + QueryData.encode(info.getFullpath(), "UTF-8") + "/" + 
								QueryData.encode(file.getName(), "UTF-8"));
						info.setImagepath(file.getName());
					
					} else if (fnlc.endsWith(".mp3") || fnlc.endsWith(".m4a") || fnlc.endsWith(".wma") || fnlc.endsWith(".flac")) {
						if (info.getSongs() == null) info.setSongs(new ArrayList <SongInfo>());
						SongInfo song = new SongInfo();
						song.setFulluri(prefix + "/" + QueryData.encode(info.getFullpath(), "UTF-8") + 
								"/" + QueryData.encode(file.getName(), "UTF-8"));
						song.setFilepath(file.getName());
						
						int sidx = file.getName().indexOf(" - ");
						String songNumber = file.getName().substring(0, sidx);
						String songName = file.getName().substring(file.getName()
								.indexOf(" - ", sidx + 3) + 2, file.getName().lastIndexOf("."));
						song.setSongnumber(songNumber);
						song.setFilename(songName);
						info.getSongs().add(song);
					}
				}
				
				if (info.getImagepath() != null) {
					String thumbfile = thumbdir + "/" + info.getImagepath();
					File thumb = new File(thumbfile);
					
					if (!thumb.exists()) {
						try {
							ImageShrinker.scale(parentDir + "/" + info.getImagepath(), 150, 150, thumbfile);
						} catch (Exception ex) {
							info.setThumb(info.getImagepath());
						}
					} 
					
					if (info.getThumb() == null) {
						info.setThumb(thumburi + "/" + QueryData.encode(info.getImagepath(), "UTF-8"));
					}
				}
			}
		}
		
		info.setPopulated(true);
	}
	
	public VersionInfo getVersion() {
		return version;
	}

	public void setParents(List<String> parents) {
		this.parents = parents;
		
		for (String parent : parents) {
			log.info("Parent Directory: " + parent);
		}
	}
}